package com.fzh.netty;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 应答客户端的业务逻辑
 * @author ZoharFang
 * @date 2016年8月31日
 */
public class EchoClientHandler extends SimpleChannelInboundHandler<String>{

	/**
	 * 通道处于激活状态时候执行
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		ctx.writeAndFlush("Netty rocks!");
	}
	
	/**
	 * 消息到达这个Handler时，Netty会自动调用这个Handler中的channelRead0
	 */
	protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
		System.out.println("Client received: " + msg);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
		ctx.close();
	}

}
